{% extends './management_base.html' %}

{% block title %}个人信息{% end %}

{% block body %}
<div class="container">
  <div class="row">
    <div class="col-lg-10">
      <div class="card dimmer">
        <div class="card-header">
          <h3 class="card-title">个人信息</h3>
        </div>
        <div class="card-body dimmer-content">
          <div class="loader"></div>
          <form>
            <div class="row">
              <div class="col-auto">
                <span class="avatar avatar-xl" style="background-image: url(/static/faces/male/33.jpg)"></span>
              </div>
              <div class="col">
                <div class="form-group">
                  <label class="form-label">用户名</label>
                  <input class="form-control" name="author_name" placeholder="用户名.." value="{{author.name}}">
                </div>
              </div>
            </div>
            <div class="form-group">
              <label class="form-label">个人说明</label>
              <textarea class="form-control" name="bio" rows="5" placeholder="个人说明..">{{author.bio}}</textarea>
            </div>
            <div class="form-group">
              <label class="form-label">邮箱地址</label>
              <input class="form-control" name="email" placeholder="邮箱.." value="{{author.email}}">
            </div>
            <div class="form-group">
              <label class="form-label">密码</label>
              <input type="password" name="password" class="form-control" value="{{author.password}}">
            </div>
            <div class="form-footer">
              <button type="button" class="btn btn-primary btn-block" onclick="saveInfo()">保存</button>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</div>
<script>
  function saveInfo() {
    let authorNameInput = $("input[name=author_name]").val()
    let bioInput = $("textarea[name=bio]").val()
    let emailInput = $("input[name=email]").val()
    let passwordInput = $("input[name=password]").val()
    // 旧密码
    let oldPassword = '{{author.password}}'

    if (authorNameInput == "" || bioInput == "" || emailInput == "" || passwordInput == "") {
      console.log("输入不能为空！")
      showAlert(status = 0, msg = "输入不能为空！")
      return false
    }
    // 加载动画
    $(".dimmer").addClass("active")
    // 发起请求
    $.ajax("/api/update_author_info", {
      method: "POST",
      contentType: "application/json",
      dataType: "json",
      headers: {},
      data: JSON.stringify({
        name: authorNameInput,
        bio: bioInput,
        email: emailInput,
        password: passwordInput
      }),
    }).done(function (data) {
      console.log(data)
      if (data["status"] == 1) {
        if (oldPassword != passwordInput){
          showAlert(1, "已更改密码，请重新登录！")
          setTimeout(() => {
            location.href = "/author/logout"
          }, 1500);
        }else{
          showAlert(1, data["msg"])
          localStorage.userName = authorNameInput
          setTimeout(() => {
            window.location.reload()
          }, 1500);
        }
      } else {
        showAlert(0, data["msg"])
        return false
      }
    }).fail(function (xhr, error) {

    }).always(() => {
      $(".dimmer").removeClass("active")
    })
  }
</script>
{% end %}